/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransitionPropertiesDialog.java
 *
 * Created on Dec 12, 2010, 2:29:38 PM
 */

package jgaf.automaton.fa;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import jgaf.Constants.MathConstants;
import jgaf.automaton.State;
import jgaf.automaton.Transition;

/**
 *
 * @author hanis
 */
public class TransitionPropertiesDialog extends javax.swing.JDialog {

    private Transition transition;
    private StateDiagramEditor editor;

    
    public TransitionPropertiesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }


    public TransitionPropertiesDialog(java.awt.Frame parent, StateDiagramEditor editor, Transition transition) {
        this(parent, true);
        this.editor = editor;
        this.transition = transition;
        init();
    }

    private void centerDialog() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
        this.setLocation(x, y);
    }

    private void init() {
        textName.setText(transition.writeLabels());
     //   textFactor.setText(String.valueOf(transition.getVisualProperties().getCurveFactor()));
        buttonFontColor.setBackground(transition.getVisualProperties().getFontColor());
        buttonStrokeColor.setBackground(transition.getVisualProperties().getStrokeColor());
        centerDialog();
    }


    private Color getChosenFontColor() {
        return buttonFontColor.getBackground();
    }

    private Color getChosenStrokeColor() {
        return buttonStrokeColor.getBackground();
    }


    private void applyChanges() throws InputFormatException {
        Transition newTransition = new Transition(transition.getFromState(), transition.getToState());
        String labels = textName.getText();
        if(labels.equals("")) {
            throw new InputFormatException("Labels has to be non-empty string.");
        }
        newTransition.setLabels(Tools.getSortedSetFromString(labels));
        newTransition.getVisualProperties().setFontColor(getChosenFontColor());
        newTransition.getVisualProperties().setStrokeColor(getChosenStrokeColor());
        newTransition.getVisualProperties().setCurveFactor(transition.getVisualProperties().getCurveFactor());
//        try {
//            newTransition.getVisualProperties().setCurveFactor(Double.valueOf(textFactor.getText()));
//        } catch (NumberFormatException e){
//            throw new InputFormatException("Curve factor has to be a real number.");
//        }
        editor.modifyTransition(transition, newTransition);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textName = new javax.swing.JTextField();
        buttonAddEps = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        buttonFontColor = new javax.swing.JButton();
        buttonStrokeColor = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        buttonCancel = new javax.swing.JButton();
        buttonOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Transition Properties");
        setBackground(java.awt.Color.white);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Basic"));

        jLabel1.setText("Labels:");

        buttonAddEps.setText("Add ε");
        buttonAddEps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddEpsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textName, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonAddEps))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonAddEps))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Colors"));

        jLabel7.setText("Font Color:");

        buttonFontColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonFontColorActionPerformed(evt);
            }
        });

        buttonStrokeColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStrokeColorActionPerformed(evt);
            }
        });

        jLabel8.setText("Stroke Color:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonStrokeColor, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                    .addComponent(buttonFontColor, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(buttonFontColor, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(buttonStrokeColor, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonCancel.setText("Cancel");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonOk.setText("Ok");
        buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(buttonOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOk)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonFontColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFontColorActionPerformed
        Color color = JColorChooser.showDialog(this, "Font color", transition.getVisualProperties().getFontColor());
        if (color != null) {
            buttonFontColor.setBackground(color);
        }
}//GEN-LAST:event_buttonFontColorActionPerformed

    private void buttonStrokeColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStrokeColorActionPerformed
        Color color = JColorChooser.showDialog(this, "Stroke color", transition.getVisualProperties().getStrokeColor());
        if (color != null) {
            buttonStrokeColor.setBackground(color);
        }
}//GEN-LAST:event_buttonStrokeColorActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        dispose();
}//GEN-LAST:event_buttonCancelActionPerformed

    
    private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        try {
            applyChanges();
            dispose();
        } catch (InputFormatException ex) {
           JOptionPane.showMessageDialog(this, ex.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_buttonOkActionPerformed

    private void buttonAddEpsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddEpsActionPerformed
        String labels = textName.getText();
//        if(labels.equals("") || !Tools.getSortedSetFromString(labels).contains(State.EPSYLON)) {
//
//        }
        if(labels.equals("")) {
            labels+=MathConstants.EPSILON;
        } else if(!Tools.getSortedSetFromString(labels).contains(MathConstants.EPSILON)) {
            labels+="," + MathConstants.EPSILON;
        }
        textName.setText(labels);
    }//GEN-LAST:event_buttonAddEpsActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TransitionPropertiesDialog dialog = new TransitionPropertiesDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAddEps;
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonFontColor;
    private javax.swing.JButton buttonOk;
    private javax.swing.JButton buttonStrokeColor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField textName;
    // End of variables declaration//GEN-END:variables

}
